package com.alibaba.service.impl;

import com.alibaba.mapper.ActivityMapper;
import com.alibaba.model.entity.Activity;
import com.alibaba.service.ActivityService;
import com.alibaba.utils.BizException;
import jakarta.annotation.Resource;
import lombok.val;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 活动服务实现类，实现ActivityService接口。
 */
@Service
public class ActivityServiceImpl implements ActivityService {

    @Resource
    private ActivityMapper activityMapper;

    @Override
    public List<Activity> listAllActivities() {
        return activityMapper.selectList(null);
    }

    @Override
    public List<Activity> listOngoingActivities() {
        LocalDateTime now = LocalDateTime.now();
        // 这里假设有合适的自定义SQL或条件构造器，实际项目可用QueryWrapper
        return activityMapper.selectList(
            new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Activity>()
                .le("start_time", now)
                .ge("end_time", now)
                .eq("status", 1)
        );
    }

    @Override
    public Activity getActivityById(Long activityId) {
        return activityMapper.selectById(activityId);
    }

    @Override
    public boolean addActivity(Activity activity) {
        val rows = activityMapper.insert(activity);
        if (rows <= 0) {
            throw new BizException("新增活动失败，activity：" + activity.toString());
        }
        return true;
    }

    @Override
    public boolean updateActivity(Activity activity) {
        int rows = activityMapper.updateById(activity);
        if (rows <= 0) {
            throw new BizException("更新活动失败，activity：" + activity.toString());
        }
        return true;
    }

    @Override
    public boolean deleteActivity(Long activityId) {
        int rows = activityMapper.deleteById(activityId);
        if (rows <= 0) {
            throw new BizException("删除活动失败，activityId：" + activityId);
        }
        return true;
    }
}
